package com.itany.corejava.code08_集合.map集合;

import java.util.*;
import java.util.Map.Entry;

/**
 * @author MissChen
 * @date 2024年03月05日 16:48
 */
public class Test05_Properties {
    public static void main(String[] args) {

        Properties properties=new Properties();
        // 继承自Hashtable，因此可以使用Hashtable的方法
        //但是开发中一般不会这么用
        // properties.put("id",1);
        // properties.put("name","admin");
        // properties.put("age",18);
        // System.out.println(properties);

        //使用Properties,一般使用的Properties自己的方法
        properties.setProperty("id","1");
        properties.setProperty("username","admin");
        properties.setProperty("password","123");
        System.out.println(properties);

        //getProperties(key)根据指定的key获取对应的值
        System.out.println(properties.getProperty("id"));
        System.out.println(properties.getProperty("username"));
        System.out.println(properties.getProperty("password"));

        //使用五种方式
        //方式一:keySet
        Set<Object> keys= properties.keySet();
        for(Object key:keys){
            String value=properties.getProperty((String)key);
            System.out.println("key:"+key+",value:"+value);
        }
        System.out.println("-------------");
        //方式二:values
        Collection<Object> values= properties.values();
        for(Object value:values){
            System.out.println(value);
        }
        System.out.println("-------------");
        //方式三:entrySet
        Set<Entry<Object,Object>> entries=properties.entrySet();
        Iterator<Entry<Object,Object>> it=entries.iterator();
        while(it.hasNext()){
            Entry<Object,Object> entry=it.next();
            Object key=entry.getKey();
            Object value=entry.getValue();
            System.out.println("key:"+key+",value:"+value);
        }
        System.out.println("-------------");
        //方式四:Enumeration遍历key
        Enumeration<Object> elements=properties.keys();
        while(elements.hasMoreElements()){
            String key=(String)elements.nextElement();
            String value=properties.getProperty(key);
            System.out.println("key:"+key+",value:"+value);
        }

        System.out.println("-------------");

        //方式五:Enumeration遍历value
        Enumeration<Object> elements2=properties.elements();
        while(elements2.hasMoreElements()){
            String value=(String)elements2.nextElement();
            System.out.println("value:"+value);
        }
    }
}
